//
//  CTRegisterViewController.m
//  CT
//
//  Created by liufanwei on 15/4/4.
//  Copyright (c) 2015年 liufanwei. All rights reserved.
//

#import "CTRegisterViewController.h"
#import "UILabel+WBExtension.h"
#import "CTNetHelper.h"
#import "BDKNotifyHUD.h"
#import "AppDelegate.h"
#import "CommonMicro.h"
#import "CTUserDefaultHelper.h"
#import "UIView+WBExtension.h"

@interface CTRegisterViewController ()<UITextFieldDelegate>{
    UIImageView * _SmallBg;
    UITextField * _PhoneNumber;
    UITextField * _PhonePWD;
    UITextField * _PhonePWDConfirm;
    UIButton    * _Commit;
    UIButton    * _Cancel;
}

@end

@implementation CTRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = kBackGroundColor;
    UILabel * title = [UILabel labelWithText: @"智云通用户注册向导" font:[UIFont fontWithName:kFontName size:20]];
    title.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = title;

    
    CGRect navigationbarframe = self.navigationController.navigationBar.frame;
    
    CGFloat x = 25;
    CGFloat y = navigationbarframe.origin.y + navigationbarframe.size.height + 10;
    _SmallBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"reg_bg"]];
    _SmallBg.userInteractionEnabled = YES;
    [self.view addSubview:_SmallBg];
    CGRect frame = _SmallBg.frame;
    frame.origin.x = x;
    frame.size.width = kAppFrameWidth - 2 * x;
    frame.origin.y = y;
    _SmallBg.frame = frame;

    
    CGFloat textfieldy = 6;
    CGFloat textfieldheight = (_SmallBg.frame.size.height - 4 * 6)/3;
    _PhoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(6, textfieldy, _SmallBg.frame.size.width - 12, textfieldheight)];
    UIImageView * numberLeftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"head"]];
    _PhoneNumber.leftView = numberLeftView;
    _PhoneNumber.leftViewMode = UITextFieldViewModeAlways;
    _PhoneNumber.backgroundColor = [UIColor whiteColor];
    _PhoneNumber.keyboardType = UIKeyboardTypeNumberPad;
    _PhoneNumber.placeholder = @"你的手机号";
    _PhoneNumber.delegate = self;
    [_PhoneNumber setFont:[UIFont fontWithName:kFontName size:21]];
    _PhoneNumber.enabled = YES;
    [_SmallBg addSubview:_PhoneNumber];
    textfieldy += 6+textfieldheight;
    
    _PhonePWD = [[UITextField alloc] initWithFrame:CGRectMake(6, textfieldy, _SmallBg.frame.size.width - 12, textfieldheight)];
    _PhonePWD.leftViewMode = UITextFieldViewModeAlways;
    _PhonePWD.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"password"]];
    _PhonePWD.backgroundColor = [UIColor whiteColor];
    _PhonePWD.placeholder = @"预设登录密码";
    _PhonePWD.delegate = self;
    [_PhonePWD setFont:[UIFont fontWithName:kFontName size:21]];
    _PhonePWD.secureTextEntry = YES;
    [_SmallBg addSubview:_PhonePWD];
    textfieldy += 6+textfieldheight;
    
    _PhonePWDConfirm = [[UITextField alloc] initWithFrame:CGRectMake(6, textfieldy, _SmallBg.frame.size.width - 12, textfieldheight)];
    _PhonePWDConfirm.leftViewMode = UITextFieldViewModeAlways;
    _PhonePWDConfirm.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"password_confirm"]];
    _PhonePWDConfirm.backgroundColor = [UIColor whiteColor];
    _PhonePWDConfirm.placeholder = @"重复登录密码";
    _PhonePWDConfirm.delegate = self;
    [_PhonePWDConfirm setFont:[UIFont fontWithName:kFontName size:21]];
    _PhonePWDConfirm.secureTextEntry = YES;
    [_SmallBg addSubview:_PhonePWDConfirm];
    
    CGFloat buttonWidth = _SmallBg.frame.size.width/2 - 15 ;
    CGFloat buttony = _SmallBg.frame.origin.y + _SmallBg.frame.size.height + 20;
    CGFloat buttonHeight = 45;
    _Commit = [[UIButton alloc] init];
    [_Commit setBackgroundImage:[UIImage imageNamed:@"commit_register"] forState:UIControlStateNormal];
    _Commit.frame = CGRectMake((x + _SmallBg.frame.size.width - buttonWidth), buttony, buttonWidth, buttonHeight);
    [_Commit addTarget:self action:@selector(onRegister:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_Commit];
    
    
    _Cancel = [[UIButton alloc] init];
    [_Cancel setBackgroundImage:[UIImage imageNamed:@"backy"] forState:UIControlStateNormal];
    _Cancel.frame = CGRectMake(x, buttony, buttonWidth, buttonHeight);
    [_Cancel addTarget:self action:@selector(onCancel:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_Cancel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)onCancel:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)onRegister:(id)sender {
    NSString * account = _PhoneNumber.text;
    NSString * pwd = _PhonePWD.text;
    NSString * cpwd = _PhonePWDConfirm.text;
    if (![cpwd isEqualToString:pwd]) {
        [BDKNotifyHUD notifyHUDWithText:@"两次密码不一致"];
        return;
    }
    if(account.length != 11 || account.intValue == 0){
        [BDKNotifyHUD notifyHUDWithText:@"非法账号！账号为你注册手机号"];
        return;
    }
    
    if (pwd.length < 6) {
        [BDKNotifyHUD notifyHUDWithText:@"密码过短"];
        return;
    }
    
    
    [[CTNetHelper ShareInstance] regiter:@{@"account":account,@"password":pwd,@"memo":@"ios",@"agentAccount":@"智云通"} WithCallBack:^(id data) {
        NSLog(@"%@",data);
        if (![data isKindOfClass:[NSDictionary class]]) {
            return;
        }
        
        NSDictionary * ret  = (NSDictionary*)data;
        [BDKNotifyHUD notifyHUDWithText:[data objectForKey:@"msg"]];
        if ([[ret objectForKey:@"code"] integerValue] == 0) {
            [[CTUserDefaultHelper ShareInstance] setAccount:account];
            [kAppDelegate ShowMain];
        }
    }];
}


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == _PhoneNumber && textField.text.length > 10 && string.length != 0) {
        return NO;
    }
    if ((textField == _PhonePWD || textField == _PhonePWDConfirm) && textField.text.length  > 11 && string.length != 0) {
        return NO;
    }
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
